/*
 * @Author: TuYongTao
 * @Date: 2021-10-25 13:41:00
 * @LastEditors: your Name
 * @LastEditTime: 2021-10-26 18:09:17
 * @Description: 
 */
const gulp = require('gulp')
const uglify = require('gulp-uglify')
const concat = require('gulp-concat')
const jshint = require('gulp-jshint')
const less = require('gulp-less')
const minifyCSS = require('gulp-cssnano')
const prefix = require('gulp-autoprefixer')
const del = require('del')
const bSync = require('browser-sync')
// const watch = require('gulp-watch')



gulp.task('test', () => {
  return gulp.src(['app/scripts/**/*.js', '!app/scripts/vendor/**/*.js']).pipe(jshint()).pipe(jshint.reporter('default')).pipe(jshint.reporter('fail'))
})
gulp.task('copy', () => {  // 定义task
  /*
  通过规则读取文件（打开流），使用pipe(管道)方法把 流 的内容传入其他函数
  gulp.dest()保存文件
  **/
  return gulp.src('app/scripts//**/*.js').pipe(gulp.dest('dist'))
})

// 压缩copy 
gulp.task('uglifyCopy', () => {
  gulp.src('app/scripts/**/*.js').pipe(uglify()).pipe(gulp.dest('dist'))
})

// 合并
gulp.task('scripts',
  gulp.series('test', function scriptsInternal() {
    return gulp.src('app/scripts/**/*.js').pipe(concat('main.min.js')).pipe(uglify()).pipe(gulp.dest('dist/scripts'))
  })
)

gulp.task('styles', () => {
  return gulp.src('app/styles/main.less').pipe(less()).pipe(minifyCSS()).pipe(prefix()).pipe(gulp.dest('dist/styles'))
})


// 清空文件
gulp.task('clear', () => {
  return del(['dist'])
})

// 通知gulp
gulp.task('report', (done) => {
  console.log('完成');
  done()
})


// 创建服务器

gulp.task('server', (done) => {
  bSync({
    server: {
      baseDir: ['dist', 'app']
    }
  })
  done()
})


// 初始化
gulp.task('default',
  gulp.series('clear', gulp.parallel('styles', 'scripts'), 'server', function watcher(done) {
    gulp.watch(['app/scripts/**/*.js', '!app/scripts/vendor/**/*.js'], gulp.parallel('scripts'));
    gulp.watch('app/styles/**/*.less', gulp.parallel('styles'));
    gulp.watch('dist/**/*', (done)=>{
      bSync.reload()
      done()
    });
    done()
  })
)

